<?php

include_once __DIR__ . "/../base.php";
class jinfu extends base
{
    protected $account;
    protected $key;
    protected $header = [
        "Content-Type:application/json"
    ];
    protected $submitUrl = "http://ipay.833006.net/tgPosp/services/payApi/wxJspay";
    protected $refundUrl = "http://ipay.833006.net/tgPosp/services/payApi/reverse";
    // protected $submitUrl = "http://ipay.833006.net/tgPosp/services/payApi/allQrcodePay";

    public function __construct($setting)
    {
        $this->account = $setting['account'];
        $this->key = $setting['key'];  //密钥
        $this->callback_url = $setting['callback_url'];
    }
    public function getSubmitSign($data)
    {
        unset($data['sign']);

        foreach ($data as $k => $v) {
            if (empty($v)) {
                unset($data[$k]);
            }
        }
        ksort($data);
        $data['key'] = $this->key;
        $str = http_build_query($data);
        $this->log("str" . urldecode($str));

        return strtoupper(md5(urldecode($str)));
    }
    public function getSgin($data)
    {
        unset($data['sign']);

        // foreach ($data as $k => $v) {
        //     if (empty($v)) {
        //         unset($data[$k]);
        //     }
        // }
        ksort($data);
        $data['key'] = $this->key;
        $str = http_build_query($data);
        $this->log("str" . urldecode($str));

        return strtoupper(md5(urldecode($str)));
    }
    // 提交支付
    public function submitOrder($data)
    {
        $order = [
            "account" => $this->account,
            "payMoney" => $data['TotalAmount'] / 100,
            "lowOrderId" => $data['OutTradeNo'],
            "body" => $data['OutTradeNo'],
            "notifyUrl" => ($this->callback_url),       // 你也可以在下单时单独设置来想覆盖它,
            "returnUrl" => ($data['success_back_page']),
            "appId" => $data['SubAppId'],
            "openId" => $data['OpenId'],
            "isMinipg" => "1",
        ];
        $order['sign'] = $this->getSubmitSign($order);
        $this->log("jinfu请求信息：");
        $this->log(json_encode($order));
        $return_str = $this->httpRequest($this->submitUrl, "POST", json_encode($order), '', '', array(
            'Content-Type: application/json'
        ));
        $this->log("jinfu响应信息：");
        $this->log($return_str);
        $return_data = json_decode($return_str, 1);


        if ($return_data['status'] == 100) {
            $json = json_decode($return_data['pay_info'], true);
            $json['timestamp'] = $json['timeStamp'];
            // $json['pay_order_sn'] = $return_data['upOrderId'];

            return array('status' => 1, 'payInfo' => $json, 'pay_order_sn' => $return_data['upOrderId']);
        } else {
            if (isset($return_data['status'])) {
                return ['status' => 0, 'msg' => "下单失败:" . $return_data['message']];
            }
        }
    }
    public function notifyOrder($params)
    {
        // {"upOrderId":"91668152730625249280","payoffType":null,"payTime":"2023-06-12 15:06:23","openid":"oeC9r5_-B9okm2tDThPnSD-YaHL8","sign":"A7DAED3FDC10CAFAD7F1158EEEA80F3B","settlementChannel":"038","lowOrderId":"CZ20230612156129195183794","payMoney":"0.01","merchantId":"623060900126479","state":"0","orderDesc":"\u652f\u4ed8\u6210\u529f","account":"36376450209330","channelId":"WX","payment_code":"jinfu","id":"2","type":"cz"}
        $resData = [
            'code' => 200,
            "data" => [],
            "msg" => "",
            "resp" => 'fail'
        ];

        $sign = $this->getSgin($params);
        if ($sign != $params['sign']) {
            $resData['code'] = 500;
            $resData['msg'] = "签名不匹配:sign" . $sign;
            return $resData;
        }
        $resData["data"] = [
            "order_sn" =>  $params['lowOrderId'],
            "pay_order_sn" => $params['upOrderId']
        ];
        $resData["resp"] = "SUCCESS";
        return $resData;
    }
    public function refund($order_sn, $refund_order_sn, $refund_price, $remark, $pay_order_sn = "")
    {

        $refund_data = [
            "account" => $this->account,
            "lowOrderId" => $order_sn,
            "upOrderId" => $pay_order_sn,
        ];
        $refund_data['sign'] = $this->getSgin($refund_data);
        $this->log("jinfu退款请求信息：");
        $this->log(json_encode($refund_data));
        $return_str = $this->httpRequest($this->refundUrl, "POST", json_encode($refund_data), '', '', $this->header);
        $this->log($order_sn . "jinfu退款响应信息：");
        $this->log($return_str);
        $return_data = json_decode($return_str, 1);

        $data = [
            "code" => 500,
            "data" => [],
            "msg" =>  ''
        ];

        if ($return_data['status'] == 100 && $return_data['state'] == 5) {
            $data['code'] = 200;
            $data['msg'] = $return_data['message'];
            return $data;
        } else {
            if (isset($return_data['status'])) {
                $data['code'] = 500;
                $data['msg'] = "退款失败，原因：" . $return_data['message'];
                return $data;
            }
        }
    }
}
